package com.partsoft.dits.exts;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.util.ObjectUtils;

public class ContextExtendsLibraryManager extends BaseExtendsLibraryManager implements ApplicationContextAware, ApplicationListener<ApplicationEvent> {
	
	private ApplicationContext ownerContext;

	public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException {
		this.ownerContext = applicationcontext;
	}
	
	protected ApplicationContext getOwnerContext() {
		return ownerContext;
	}

	public void onApplicationEvent(ApplicationEvent event) {
		if (!ObjectUtils.nullSafeEquals(getOwnerContext(), event.getSource())) return;
		if (event instanceof ContextRefreshedEvent) {
			initContext();
		} else if (event instanceof ContextStoppedEvent) {
			finiContext();
		}
	}

}
